using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Scrobbler
{
    public class ConfigurationHandler
    {
        public static IConfiguration Load(string path)
        {
            using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
            {
                var formatter = new BinaryFormatter();
                return (IConfiguration) formatter.Deserialize(fs);
            }
        }

        public static void Save(IConfiguration configuration, string path)
        {
            using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(fs, configuration);
            }
        }
    }
}